import Mock from "mockjs";

// 创建数据
let { data } = Mock.mock({
  // 20条数据
  "data|126": [
    {
      "id|+1": 1,
      name: "@name",
      doctor: "@cname",
      "type|1": ["狗", "猫"],
      "age|1-150": 1,
      "phone|13000000000-18999999999": 13000000000,
      "time|1577836800000-1670830434813": 1577836800000,
    },
  ],
});

// 列表接口，分页，搜索
Mock.mock("/api/pos/list", "post", (options) => {
  const { pageSize, currentPage, search } = JSON.parse(options.body);

  if (pageSize && currentPage) {
    let result;
    let count = 0;
    if (search) {
      result = data
        .filter((item: any) => item.doctor === search)
        .slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.filter((item: any) => item.doctor === search).length;
    } else {
      result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.length;
    }
    return {
      status: 0,
      result: result,
      count: count,
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});

// 删除接口
Mock.mock("/api/pos/del", "post", (options) => {
  const { id } = JSON.parse(options.body);
  if (id) {
    data = data.filter((item: any) => item.id !== id);
    return {
      status: 0,
      msg: "删除成功",
    };
  } else {
    return {
      status: 1,
      msg: "参数错误",
    };
  }
});

// 下载接口
Mock.mock("/api/pos/export", "post", () => {
  return {
    status: 0,
    result: data,
  };
});

export default Mock;
